package ibatis;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.Reader;

import util.AttendConsts;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class SqlConfig {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SqlConfig.class);

	private static SqlMapClient sqlMap 		= null;
	private static SqlConfig 	instance_ 	= null;

	/**
	 * constructor
	 * @throws Exception
	 */
	private SqlConfig() throws Exception {
		Reader 			reader 		= null;
//		StringBuffer 	rsc 		= null;
		
		try {
			if (sqlMap == null) {
//				rsc = new StringBuffer(200);
//				rsc.append("com").append(File.separator);
//				rsc.append("jaeminara").append(File.separator);
//				rsc.append("dwr").append(File.separator);
//				rsc.append("app").append(File.separator);
//				rsc.append("sql").append(File.separator).append(AttendConsts.SQLMAP_CONFIG_XML);
				
//				reader = Resources.getResourceAsReader(rsc.toString());
//				reader = Resources.getResourceAsReader(AttendConsts.SQLMAP_CONFIG_XML);
				reader = Resources.getResourceAsReader("sqlMapConfig.xml");
				
				
				sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
				
				reader.close();
			}
		} catch (Exception e) {
//			System.out.println(e);
			logger.error(e);
			throw e;
		} finally {
			if (reader != null)				reader.close();
			reader 		= null;
//			rsc 		= null;
		}
	}

	/**
	 * @return SqlConfig
	 */
	public static SqlConfig instance() {
		try {
			if (instance_ == null) {
				synchronized (SqlConfig.class) {
					if (instance_ == null) instance_ = new SqlConfig();
				}
			}
		} catch (Exception e) {
//			System.out.println(e);
			logger.error(e);
		}
		return instance_;
	}

	/** * Return SqlMapClient for SDP schema * * @return */
	public SqlMapClient getSqlMapInstance() {
		return sqlMap;
	}
}